package br.com.iswe.gwt.ui.client.comp;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusListenerCollection;
import com.google.gwt.user.client.ui.HasFocus;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerCollection;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.impl.FocusImpl;

public class ISWEButton extends Widget implements SourcesClickEvents, HasFocus {
	private static final String ISWE_BTN_TEXT = "iswe-btn-text";

	private static final String CLASS_NAME = "className";

	private static final String SPACE = " ";

	private static final String ISWE_BTN_CLICK = "iswe-btn-click";

	private static final String ISWE_BTN_OVER = "iswe-btn-over";

	private static final String ISWE_ITEM_DISABLED = "iswe-item-disabled";

	private static final String ISWE_BTN = "iswe-btn";

	static long idButtons = -1;

	protected ClickListenerCollection clickListeners = new ClickListenerCollection();

	protected String overStyle = ISWE_BTN_OVER + SPACE + ISWE_BTN;

	protected String downStyle = ISWE_BTN_CLICK + SPACE + ISWE_BTN_OVER + SPACE
			+ ISWE_BTN;

	protected String normalStyle = ISWE_BTN;

	protected String disabledClasses = ISWE_ITEM_DISABLED + SPACE + ISWE_BTN;

	protected String idTable = null;

	protected String idButton = null;

	protected boolean enabled = true;

	protected Element divElement = null;

	protected String html = null;

	private String width = null;

	private static final FocusImpl impl = FocusImpl.getFocusImplForWidget();

	public ISWEButton(String html) {
		idButtons++;// incrementa o id dos objetos
		idTable = "__isweBTB_" + idButtons;
		idButton = "__isweBBN_" + idButtons;

		this.html = html;

		divElement = DOM.createDiv();

		buildDivInnerHtml();
		setElement(divElement);

		// capturar o evento de onclick
		sinkEvents(Event.ONCLICK);
	}

	private void buildDivInnerHtml() {
		String htmlObject = "<table id='" + idTable
				+ "' border=0 cellpadding=0 cellspacing=0 ";
		// enabled
		if (enabled) {
			htmlObject += " class='" + ISWE_BTN + "' ";
		} else {
			// caso esteja desabilitado
			htmlObject += " class='" + ISWE_ITEM_DISABLED + SPACE + ISWE_BTN
					+ "' ";
		}
		// Table Events
		htmlObject += " onmouseover=\"if(document.getElementById('" + idTable
				+ "').className=='" + disabledClasses
				+ "')return;document.getElementById('" + idTable
				+ "').className='" + overStyle + "'\" ";
		htmlObject += " onmouseup=\"if(document.getElementById('" + idTable
				+ "').className=='" + disabledClasses
				+ "')return;document.getElementById('" + idTable
				+ "').className='" + overStyle + "'\" ";
		htmlObject += " onmousedown=\"if(document.getElementById('" + idTable
				+ "').className=='" + disabledClasses
				+ "')return;document.getElementById('" + idTable
				+ "').className='" + downStyle + "'\" ";
		htmlObject += " onmouseout=\"if(document.getElementById('" + idTable
				+ "').className=='" + disabledClasses
				+ "')return;document.getElementById('" + idTable
				+ "').className='" + normalStyle + "'\" >";
		htmlObject += " <tr><td class='iswe-btn-left'><i>&nbsp;</i></td><td class='iswe-btn-center'>\n";
		// Button
		htmlObject += " <button id='" + idButton + "' class='" + ISWE_BTN_TEXT + "' ";
		if(this.width != null){
			htmlObject += " style='width:" + this.width + ";' ";
		}
		htmlObject += " type='button'>" + this.html + "</button> \n";

		htmlObject += " </td><td class='iswe-btn-right'><i>&nbsp;</i></td></tr></table>";

		DOM.setInnerHTML(divElement, htmlObject);
	}

	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.ONCLICK:
			if (enabled && clickListeners != null) {
				clickListeners.fireClick(this);
			}
			break;
		}
	}

	public ISWEButton() {
		this("");
	}

	public void setHTML(String html) {
		setText(html);
	}
	
	public void setWidth(String width) {
		this.width  = width;
		if (getButtonElement() != null) {
			DOM.setStyleAttribute(getButtonElement(), "width", width);
		} else {
			// caso ainda nao tenha sido adicionado na arvore.. re-cria o html
			// baseado nas novas propriedades.
			buildDivInnerHtml();
		}
	}

	public void setText(String html) {
		this.html = html;
		if (getButtonElement() != null) {
			DOM.setInnerHTML(getButtonElement(), html);
		} else {
			// caso ainda nao tenha sido adicionado na arvore.. re-cria o html
			// baseado nas novas propriedades.
			buildDivInnerHtml();
		}
	}

	private Element getButtonElement() {
		return DOM.getElementById(idButton);
	}

	private Element getTableElement() {
		return DOM.getElementById(idTable);
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
		if (getTableElement() != null) {
			// caso ja tenha sido inserido na arvode dom.
			if (!this.enabled) {
				// caso esteja desabilitando
				DOM.setElementProperty(getTableElement(), CLASS_NAME,
						disabledClasses);
				DOM.setElementProperty(getTableElement(), "disabled", "true");
			} else {
				DOM.removeElementAttribute(getTableElement(), "disabled");
				DOM.setElementProperty(getTableElement(), CLASS_NAME, ISWE_BTN);
			}
		} else {
			// caso ainda nao tenha sido adiciona na arvore.. re-cria o html
			// baseado nas novas propriedades.
			buildDivInnerHtml();
		}
	}

	public void addClickListener(ClickListener clickListener) {
		if(clickListener == null){
			clickListener = null;
			return;
		}
		clickListeners.add(clickListener);
	}

	public void removeClickListener(ClickListener listener) {
		clickListeners.remove(listener);
	}

	public int getTabIndex() {
		return impl.getTabIndex(getButtonElement());
	}

	public void setAccessKey(char key) {
		DOM.setElementProperty(getButtonElement(), "accessKey", "" + key);
	}

	public void setFocus(boolean focused) {
		if (focused) {
			impl.focus(getButtonElement());
		} else {
			impl.blur(getButtonElement());
		}
	}

	public void setTabIndex(int index) {
		impl.setTabIndex(getButtonElement(), index);
	}

	protected FocusListenerCollection focusListeners = null;

	public void addFocusListener(FocusListener listener) {
		if (focusListeners == null) {
			focusListeners = new FocusListenerCollection();
		}
		focusListeners.add(listener);
	}

	public void removeFocusListener(FocusListener listener) {
		if (focusListeners != null) {
			focusListeners.remove(listener);
		}
	}

	protected KeyboardListenerCollection keyboardListeners = null;

	public void addKeyboardListener(KeyboardListener listener) {
		if (keyboardListeners == null) {
			keyboardListeners = new KeyboardListenerCollection();
		}
		keyboardListeners.add(listener);
	}

	public void removeKeyboardListener(KeyboardListener listener) {
		if (keyboardListeners != null) {
			keyboardListeners.remove(listener);
		}
	}

	public void click() {
		clickListeners.fireClick(this);
	}
}
